<template>
	<div>
		<el-row class="bg">
			<el-col :span="24">
				<el-form :inline="true" :model="dataForm" ref="dataForm">
					<el-form-item prop="dcu" class="doorNuTxt" label="门控单元编号："
						style="margin-right: 2.6vw; margin-bottom: 0">
						<el-select v-model="dataForm.dcu" filterable multiple collapse-tags clearable placeholder="请选择"
							style="width: 12vw;min-width: 155px;" @change="doorChange">
							<el-checkbox v-model="checkOptions.checkAll" :style="{ padding: '5px 20px' }"
								:indeterminate="checkOptions.isIndeterminate" @change="handleCheckAllChange">
								全选
							</el-checkbox>
							<el-option v-for="(item, index) in options" :key="index" :label="item.dcu"
								:value="item.dcu"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item class="baseInfoBtnRight">
						<el-button class="baseInfoBtn1" type="primary" @click="getDateData">读取
						</el-button>
					</el-form-item>
				</el-form>

			</el-col>
		</el-row>
		<el-row class="bg1">
			<el-table v-loading="dataListLoading" :data="dataList" border style="width: 100%;" class="formBox">
				<el-table-column prop="dcuId" label="dcuId" header-align="center" align="center">
					<template slot-scope="scope">
						<span v-if="scope.row.dcuId<129">{{Number(scope.row.dcuId)+stepUp}}</span>
						<span v-else>{{Number(scope.row.dcuId) - 128 + stepDown}}</span>
					</template>
				</el-table-column>
				<el-table-column prop="cpu0" label="cpu1版本号" header-align="center" align="center"></el-table-column>
				<el-table-column prop="cpu1" label="cpu2版本号" header-align="center" align="center"></el-table-column>
			</el-table>
			<!-- <el-pagination background :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit"
				:total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle"
				@current-change="pageCurrentChangeHandle">
			</el-pagination> -->
		</el-row>
		<el-row class="bg2 bg3">
			<el-col :span="24">
				<div class="dcuTxt">
					<h3 class="baseHeader" style="text-align: left">DCU固件文件</h3>
					<div>
						<el-upload :data="paramsData" :headers="headers" :on-success="handleSuccess" :action="dcuurl"
							class="upload-demo" :show-file-list="false" :file-list="fileList" style="text-align: left;">
							<el-button type="primary">
								上传固件
							</el-button>
						</el-upload>
						<!-- <el-button class="newAdd" v-if="$hasPermission('sys:user:save')" type="primary"
							@click="addOrUpdateHandle()">新增</el-button> -->
					</div>
				</div>
				<div>
					<el-table v-loading="dataListLoading1" :data="dataList1" border
						@selection-change="dataListSelectionChangeHandle" @sort-change="dataListSortChangeHandle"
						style="width: 100%">
						<el-table-column type="selection" header-align="center" align="center" width="50">
						</el-table-column>
						<el-table-column type="index" label="序号" header-align="center" align="center" width="50">
						</el-table-column>
						<el-table-column prop="name" label="DCU固件名称" header-align="center" align="center">
						</el-table-column>
						<el-table-column prop="packagePath" label="DCU固件路径" header-align="center" align="center">
						</el-table-column>
						<el-table-column prop="createDate" label="登记日期" header-align="center" align="center"
							width="180"></el-table-column>
						<!-- <el-table-column prop="sysUserName" label="登记人" header-align="center" align="center">
						</el-table-column> -->
						<el-table-column prop="doorList" label="失败门编号" header-align="center" align="center">
							<template slot-scope="scope">
								{{ scope.row.doorList }}
								<!-- <div class="progress-bar">
									<div class="progress-bg" :style="{ width: jdNumber }"></div>
								</div> -->
							</template>
						</el-table-column>
						<el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center"
							width="300">
							<template slot-scope="scope">
								<div style="display: flex;justify-content:space-around;padding: 0 20px;">
									<el-button type="primary" round size="mini" :disabled="btnStatus"
										@click="upgrade(scope.row.packagePath, scope.row.id, false)">
										升级DCU</el-button>

									<!-- <el-button type="danger" round size="mini" @click="deleteHandleDCU(scope.row.id)">
										{{ $t("delete") }}
									</el-button> -->
								</div>
							</template>
						</el-table-column>
					</el-table>
					<add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDCUpackageList">
					</add-or-update>
					<dcuUpgrade v-if="dcuUpgtadeVisible" ref="dcuUpgrade" @clearData="clearData" :jdNumber="jdNumber"
						@disabledTxt="disabledTxt">
					</dcuUpgrade>
				</div>
			</el-col>
		</el-row>
	</div>
</template>

<script>
	import {
		getOrder,
		getStep
	} from "@/utils";
	import dcuUpgrade from "../setting/dcuUpgrade.vue";
	import Cookies from "js-cookie";
	import qs from 'qs'
	import mixinViewModule from "@/mixins/view-module";
	export default {
		mixins: [mixinViewModule],
		components: {
			dcuUpgrade,
		},
		data() {
			return {
				dataForm: {
					dcu: ''
				},
				options: [],
				checkOptions: {
					isIndeterminate: false,
					checkAll: false,
				},
				stepUp: 0, //上行门的step值
				stepDown: 0, //下行门的step值
				dataListLoading: false,
				page: 1,
				limit: 10,
				total: 0,
				dataList: [],
				dcuUpgtadeVisible:true,
				paramsData: {
					type: 'dcu'
				},
				headers: {
					'Authorization': Cookies.get('token')
				},
				type: Cookies.get("type"), //Cookies.get('type')
				dcuurl: window.SITE_CONFIG['apiURL'] + '/psc/packagemanage/uploader',
				fileList: [],
				dataListLoading1 : false,
				dataList1: [],
				jdNumber: "",
				mixinViewModuleOptions: {
					createdIsNeed: false,
					deleteURL: "",
					deleteIsBatch: true,
					activatedIsNeed: false,
					exportURL: "/psc/dcuparameters/export",
				},
				btnStatus: false,
				stationSide:0
			}
		},
		activated() {
			this.init();
		},
		methods: {
			init() {
				var p0 = new Promise((resolve, reject) => {
					this.getdoorList(() => {
						resolve();
					});
				});
				var p1 = new Promise((resolve, reject) => {
					this.getDCUpackageList(() => {
						resolve();
					});
				});
				Promise.all([p0,p1]).then(() => {
					// this.getDCUData()
					// this.initEcharts()
					this.handleMsg()
				});
			},

			handleMsg() {
				this.$globalWebSocket.ws1.send("DCU");
				this.$globalWebSocket.ws1.onmessage = this.getMessage;
			},

			//获取通讯
			getMessage(e) {
				var message = JSON.parse(e.data);
				if (message.process !== undefined) {
					this.jdNumber = message.process + '%'
				}
			},

			//读取生产日期
			getDateData() {
				var dcuId = [];
				if (this.dataForm.dcu.length == 0) {
					this.$message.error('请选择门控单元编号');
					return false
				} else {
					if (this.checkOptions.checkAll) {
						dcuId.push('FF')
					} else {
						this.dataForm.dcu.forEach(value => {
							const findIntem = this.options.find(item => item.dcu === value);
							if (findIntem.stationSide == 1) {
								var dcuIdNew = value - this.stepUp;
								dcuId.push(dcuIdNew)
							}
							if (findIntem.stationSide == 0) {
								var dcuIdNew = Number(value) - this.stepDown + 128;
								dcuId.push(dcuIdNew)
							}
						})
					}
					this.getdoorVersion(dcuId);
				}
			},
			//获取版本号
			getdoorVersion(dcuId) {
				let _this = this
				this.$http.post(`/psc/dcu/readVersionAll`, dcuId).then((res) => {
					if (res.data.code !== 0) {
						return this.$message.error(res.data.msg);
					}
					this.$message({
						message: this.$t("prompt.success"),
						type: "success",
						duration: 500,
						onClose: () => {
							this.dataList = res.data.data
						},
					});
				});
			},
			//改变门的编号
			doorChange() {
				const nowLength = this.dataForm.dcu.length;
				this.checkOptions.isIndeterminate =
					nowLength > 0 && nowLength < this.options.length;
				this.checkOptions.checkAll = nowLength == this.options.length;
				this.$forceUpdate();
			},
			// 流向全选
			handleCheckAllChange(e) {
				this.dataForm.dcu = e ? this.options.map((item) => item.dcu) : [];
				this.checkOptions.isIndeterminate = false;
			},
			getdoorList(callback) {
				this.$http
					.get("/psc/door/getDoor")
					.then((res) => {
						//获取上下行数据要增加和减少的值-----2023年5月26日
						this.stepUp = getStep(res.data.data.up)
						this.stepDown = getStep(res.data.data.down)
						this.options = res.data.data.up.concat(res.data.data.down);
						for (var i = 0; i < this.options.length; i++) {
							if (
								this.options[i].doorType === 0 ||
								this.options[i].doorType === 2
							) {
								this.options.splice(i, 1); // 将使后面的元素依次前移，数组长度减1
								i--; // 如果不减，将漏掉一个元素
							}
						}
						if (callback) {
							callback();
						}
					})
					.then(() => {
						// this.getDCUData()
					});
			},
			handleSuccess(res, file, fileList) {
				if (res.code == 0) {
					this.$message({
						message: '上传成功',
						type: 'success',
						duration: 500,
						onClose: () => {
							this.getDCUpackageList()
						}
					})
				}
			},
			//获取dcu升级包
			getDCUpackageList(callback) {
				this.dataListLoading1 = true;
				this.dataList1 = []
				this.$http
					.get("/psc/packagemanage/dcu", {}).then(({
						data: res
					}) => {
						this.dataListLoading1 = false;
						if (res.code !== 0) {
							this.dataList1 = [];
							this.total = 0;
							return this.$message.error(res.msg);
						}
						this.dataList1.push(res.data)
						// this.dataList = res.data.list;
						// this.total = res.data.total;
						this.dataList1.forEach((item, index) => {
							this.$set(this.dataList1[index], "doorList", "");
						});
						callback();
					});
			},
			//每次升级dcu,清空一下失败门编号
			clearData(val) {
				if (val == 1) {
					this.dataList.forEach((item, index) => {
						if (item.id == this.updateId) {
							this.$set(this.dataList[index], "doorList", ''); //获取失败的门编号
						}
					});
				}
			},
			disabledTxt(val) {
				this.btnStatus = val
				setTimeout(() => {
					this.btnStatus = false
				}, 20 * 1000)
			},
			//升级DCU
			upgrade(path, id, status) {
				this.Arr = []
				this.dcuUpgtadeVisible = true;
				this.updateId = id;
				this.$nextTick(() => {
					this.$refs.dcuUpgrade.init(path, status);
				});
			},
		}
	}
</script>

<style scoped>
	.bg {
		background-color: #fff;
		border-radius: 10px;
		display: flex;
		align-items: center;
		/* height: 7.6vh; */
		/* min-height: 76px; */
		/* line-height: 7.6vh; */
		padding: 0.8vw 1.8vw;
	}

	.bg>>>.el-form--inline .el-form-item__content,
	.el-form--inline .el-form-item {
		vertical-align: middle !important;
	}

	.bg>>>.el-form-item {
		margin-bottom: 0;
	}

	.bg1 {
		background-color: #fff;
		border-radius: 10px;
		padding: 1.6vh 1.5vw;
		margin-top: 10px;
	}
	.bg2 {
		background-color: #fff;
		border-radius: 10px;
		padding: 2.1vh 1.5vw;
		margin-top: 10px;
		height: 38.1vh;
	}

	.bg3 {
		height: auto !important;
	}
</style>